<script type="text/javascript">
	$(document).ready(function(){
		$.validator.messages.required = "";
		$("#FormThem").validate();
	});
</script>
<?php
	if(isset($_GET['mhs']))
		$MaHocSinh = $_GET['mhs'];
?>
<h3>Nhập điểm</h3>
<form id="FormThem" action="xeploaihk-<?php echo $MaHocSinh; ?>" method="post">
<input type="hidden" value="<?php echo $MaHocSinh; ?>" name="MaHocSinh" />
	<table class="Form">
		<tr>
        
        	<td>
            	Năm học: 
            </td>    
             <td>   <select name="NamHoc">
                			<?php
                            $namhoc = new namhocbs();
                            $dmnamhoc = $namhoc->GetListNamHoc();
                            
                            for($i = 0; $i < count($dmnamhoc); $i++)
                            {       
                                    echo "<option value='{$dmnamhoc[$i]['NamHoc']}'>{$dmnamhoc[$i]['NamHoc']}</option>";
                            }
                    		?>
                		 </select>
            </td>
           </tr>
           <tr>
            <td>
            	Học kỳ: 
             </td>
             <td>  <select name="HocKy">
                			<option value='Học kỳ I'>Học Kỳ I</option>
                            <option value='Học kỳ I'>Học Kỳ II</option>
                		 </select>
                         
            </td>   
        </tr>
		<tr>
			<td width="40%">Hạnh kiểm:</td>
			<td><select name="MaHanhKiem">
                			<?php
                            $hanhkiem = new hanhkiembs();
                            $dmhanhkiem = $hanhkiem->GetList();
                            
                            for($i = 0; $i < count($dmhanhkiem); $i++)
                            {       
                                    echo "<option value='{$dmhanhkiem[$i]['MaHanhKiem']}'>{$dmhanhkiem[$i]['HanhKiem']}</option>";
                            }
                    		?>
                		 </select><span  class="requirefield">*</span></td>
		</tr>
         <tr>
        	<td colspan="2">
            	<p class="requirefields">(*): Trường bắt buộc phải nhập thông tin.</p>
				<input type="submit" value="Thêm" name="btnThem"/>
            </td>
        </tr>
	</table>
</form>
<?php
	if(isset($_POST['btnThem']))
	{
		$MaHocSinh =  $_POST['MaHocSinh'];
		$HocKy = $_POST['HocKy'];
		$NamHoc = $_POST['NamHoc'];
		$MaHanhKiem = $_POST['MaHanhKiem'];
		
		$DTB_HK = round(DTB_HK($MaHocSinh, $NamHoc, $HocKy),2);
	
		
		
		$xeploai = new xeploaihkbs();
		$xeploai->MaHocSinh = $MaHocSinh;
		$xeploai->HocKy = $HocKy;
		$xeploai->NamHoc = $NamHoc;
	
		$kt = $xeploai->GetList();
		
		$xeploai->MaHocSinh = $MaHocSinh;
		$xeploai->HocKy = $HocKy;
		$xeploai->NamHoc = $NamHoc;
		$xeploai->MaHanhKiem = $MaHanhKiem;
		$xeploai->DiemTBHK = $DTB_HK;
		if($DTB_HK >= 8)
		{
			if(LoaiGioi($MaHocSinh, $NamHoc, $HocKy))
				$xeploai->XepLoaiHK = "Giỏi";
			else
				$xeploai->XepLoaiHK = "Khá";
		}elseif($DTB_HK >= 6.5 && $DTB_HK < 7.9)
		{
			if(LoaiKha($MaHocSinh, $NamHoc, $HocKy))
				$xeploai->XepLoaiHK = "Khá";
			else
				$xeploai->XepLoaiHK = "Trung bình";
		}elseif($DTB_HK >= 5 && $DTB_HK < 6.4)
		{
			if(LoaiTB($MaHocSinh, $NamHoc, $HocKy))
				$xeploai->XepLoaiHK = "Trung bình";
			else
				$xeploai->XepLoaiHK = "Yếu";
		}elseif($DTB_HK >= 3.5 && $DTB_HK < 4.9)
		{
			if(LoaiYeu($MaHocSinh, $NamHoc, $HocKy))
				$xeploai->XepLoaiHK = "Yếu";
			else
				$xeploai->XepLoaiHK = "Kém";
		}else
			$xeploai->XepLoaiHK = "Kém";
		if($kt)			
			$kq=$xeploai->Update();
		else			
			$kq=$xeploai->Add();
		if($HocKy == "Học kỳ II")
		{
			$xeploai = new xeploaihkbs();
			$xeploai->MaHocSinh = $MaHocSinh;
			$xeploai->NamHoc = $NamHoc;
			$dmxeploai = $xeploai->GetList();
			
			$xeploaicn = new xeploaicnbs();
			$xeploaicn->MaHocSinh =$MaHocSinh;
			$xeploaicn->NamHoc =$NamHoc;
			$dmxeploaicn = $xeploaicn->GetList();
			$xeploaicn->DiemTBCN = round( DTBMH_CN($dmxeploai[0]['DiemTBHK'],$dmxeploai[1]['DiemTBHK']),2);
			$xeploaicn->MaHanhKiem = XetHanhKiem($dmxeploai[0]['MaHanhKiem'],$dmxeploai[1]['MaHanhKiem']);
			if($xeploaicn->DiemTBCN >= 8)
			{
				if(LoaiGioi($MaHocSinh, $NamHoc, $HocKy))
					$xeploaicn->XepLoaiCN = "Giỏi";
				else
					$xeploaicn->XepLoaiCN = "Khá";
			}elseif($xeploaicn->DiemTBCN >= 6.5 && $xeploaicn->DiemTBCN < 7.9)
			{
				if(LoaiKha($MaHocSinh, $NamHoc, $HocKy))
					$xeploaicn->XepLoaiCN = "Khá";
				else
					$xeploaicn->XepLoaiCN = "Trung bình";
			}elseif($xeploaicn->DiemTBCN >= 5 && $xeploaicn->DiemTBCN < 6.4)
			{
				if(LoaiTB($MaHocSinh, $NamHoc, $HocKy))
					$xeploaicn->XepLoaiCN = "Trung bình";
				else
					$xeploaicn->XepLoaiCN = "Yếu";
			}elseif($xeploaicn->DiemTBCN >= 3.5 && $xeploaicn->DiemTBCN < 4.9)
			{
				if(LoaiYeu($MaHocSinh, $NamHoc, $HocKy))
					$xeploaicn->XepLoaiCN = "Yếu";
				else
					$xeploaicn->XepLoaiCN = "Kém";
			}else
				$xeploaicn->XepLoaiCN = "Kém";
			if($dmxeploaicn)
				$xeploaicn->Update();
			else
				$xeploaicn->Add();
		}
		if($kq)
			ThongBao("Đã xếp loại");
		else
			ThongBaoLoi("Lõi truy vấn");
			

	}
?>